package sunwin.yois.web.controller;

import jodd.util.StringUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sunwin.yois.web.common.R;
import sunwin.yois.web.common.YoisImageHelper;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.domain.resource.StadiumDomain;
import sunwin.yoms.domain.resource.StadiumGroupDomain;
import sunwin.yoms.service.resource.StadiumGroupService;
import sunwin.yoms.service.resource.StadiumService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mt on 2015-9-22.
 */
@Controller
public class StadiumController {
    @Autowired
    private StadiumService stadiumService;
    @Autowired
    private StadiumGroupService stadiumGroupService;

    /**
     * 场馆首页
     *
     * @param modelMap
     */
    @RequestMapping(value = R.SubscribeIndex, name = "subscription/", method = RequestMethod.GET)
    public void subscribeIndex(ModelMap modelMap) {
        //获取热门场馆
        getStadiumHot(modelMap);

        //获取场馆分组
        getStadiumGroups(modelMap);
    }

    /**
     * 场馆介绍
     *
     * @param modelMap
     * @param sid
     */
    @RequestMapping(value = R.SubscribeDetailed, name = "subscription/", method = RequestMethod.GET)
    public void stadiumDetailed(ModelMap modelMap, int sid) throws YomsException {
        modelMap.addAttribute("sid", sid);

        stadiumService.updateStadium(sid);

        StadiumDomain domain = stadiumService.getStadiumById(sid);
        modelMap.addAttribute("stadiumName", domain.getStadiumName());

        String content = YoisImageHelper.changeImgSrc(domain.getStadiumContent(), "detailed");
        domain.setStadiumContent(content);

        if (StringUtil.isBlank(domain.getStadiumContent())) {
            domain.setStadiumContent("...");
        }
        modelMap.addAttribute("domain", domain);
    }

    /**
     * 获取热门场馆
     */
    private void getStadiumHot(ModelMap modelMap) {
        List<StadiumDomain> stadiums = new ArrayList<>();

        stadiums = stadiumService.findStadiumHot(5);
        modelMap.addAttribute("stadiumsHot", stadiums);
    }

    /**
     * 根据类型获取场馆
     *
     * @param tid
     * @return
     */
    @RequestMapping(value = R.GetValidStadium, method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public Object getValidStadium(int tid) {
        List<StadiumDomain> stadiums = stadiumService.getStadiumByGroup(tid);
        for (StadiumDomain domain : stadiums) {
            domain.setStadiumStarttime(null);
            domain.setStadiumEndtime(null);
            domain.setStadiumContent(null);
        }

        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(stadiums);

        return jsonArray.toString();
    }

    /**
     * 获取场馆分组
     *
     * @param modelMap
     */
    private void getStadiumGroups(ModelMap modelMap) {
        List<StadiumGroupDomain> groups = new ArrayList<>();
        try {
            groups = stadiumGroupService.getStadiumGroup(null);
        } catch (Exception e) {

        }
        modelMap.addAttribute("groups", groups);
    }
}
